﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Tetriphone
{
    public partial class MainPage : PhoneApplicationPage
    {

        const int FIELDWIDTH = 12;
        const int FIELDHEIGHT = 22;
        int[,] field1 = new int[FIELDWIDTH, FIELDHEIGHT];

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Tetriphone.netcode.Instance.MessageCallback = MessageCallback;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Tetriphone.netcode.Instance.ConnectToServer("192.168.0.185", 31457);
        }

        private void MessageCallback(string message)
        {
            if(message.StartsWith("f"))
            {
                ProcessField(message);
            }
            
        }

        void ProcessField(string message)
        {
            string[] fieldData = message.Split(' ');
            if (fieldData.Length != 3)
            {
                return;
            }
            if (fieldData[1] == "1")
            {
                // its our field
                if ((int)fieldData[2][0] >= (int)'0')
                {
                    // they have sent the whole field
                    for (int y = 0; y < FIELDHEIGHT; y++)
                        for (int x = 0; x < FIELDWIDTH; x++)
                            field1[x, y] = (int)fieldData[2][y * FIELDWIDTH + x] - (int)'0';

                }
                else
                {
                    // they've just sent an update
                    int j, x, y, block = 0;

                    for (j = 0; j < FIELDWIDTH * FIELDHEIGHT && j < fieldData[2].Length; j++)
                    {
                        /* block information? */
                        if ((int)fieldData[2][j] >= (int)'!' && (int)fieldData[2][j] < (int)'0')
                            block = (int)fieldData[2][j] - (int)'!';
                        else
                        /* position */
                        {
                            x = (int)fieldData[2][j] - (int)'3'; j++;
                            y = (int)fieldData[2][j] - (int)'3';
                            if (x >= 0 && x < FIELDWIDTH && y >= 0 && y < FIELDHEIGHT)
                                field1[x, y] = block;
                        }
                    }
                }
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {

                  playingField1.UpdateField(field1);
                  playingField2.UpdateField(field1);
                  playingField3.UpdateField(field1);
                });


            }
        }
    }
}